package com.bootdo.staticmask.service.impl;

import com.bootdo.system.controller.LoginController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.staticmask.dao.MaskFileDao;
import com.bootdo.staticmask.domain.MaskFileDO;
import com.bootdo.staticmask.service.MaskFileService;

@Service
public class MaskFileServiceImpl implements MaskFileService {
	private final MaskFileDao maskFileDao;

	@Autowired
	public MaskFileServiceImpl(MaskFileDao maskFileDao) {
		this.maskFileDao = maskFileDao;
	}

	@Override
	public MaskFileDO get(Integer fileId){
		return maskFileDao.get(fileId);
	}
	
	@Override
	public List<MaskFileDO> list(Map<String, Object> map){
		return maskFileDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return maskFileDao.count(map);
	}
	
	@Override
	public int save(MaskFileDO maskFile){
		return maskFileDao.save(transform(maskFile));
	}
	
	@Override
	public int update(MaskFileDO maskFile){
		return maskFileDao.update(transform(maskFile));
	}
	
	@Override
	public int remove(Integer fileId){
		return maskFileDao.remove(fileId);
	}
	
	@Override
	public int batchRemove(Integer[] fileIds){
		return maskFileDao.batchRemove(fileIds);
	}

	/**
	 * 前台双引号和单引号、无和数据库的转换。 以及增加用户和时间
	 * @param maskFile maskFile
	 * @return maskFile
	 */
	private MaskFileDO transform(MaskFileDO maskFile){
		maskFile.setCreateuser(LoginController.getUSER());
		maskFile.setCreatetime(new Date());
		switch (maskFile.getTextQualifier()){
			case "double":maskFile.setTextQualifier("\"");break;
			case "single":maskFile.setTextQualifier("'");break;
			case "none":maskFile.setTextQualifier(" ");break;
			default:break;
		}
		return maskFile;
	}
}
